using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using TaskManager.Common;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO;
using DBUtility;
using System.Data.Odbc;
using MySQL;
using Tools;

namespace TaskManager.temp
{
    public class ManageUser
    {
        private OdbcConnection CnMySQL = new OdbcConnection("driver={mysql odbc 5.1 driver};database=nvren;server=127.0.0.1;uid=root;pwd=;Stmt=Set Names 'GBK'");

        public void GetAllUser()
        {

            string TargetUrl = @"http://blog.sina.com.cn/lm/search/class/index.html";

            Encoding _PageEncode = Encoding.GetEncoding(@"gb2312");
            Stream webContent = Tools.WebUtility.GetStreamFromUrl(TargetUrl, TaskConfig.MAXTIMEOUT);
            StreamReader sr = new StreamReader(webContent, _PageEncode);
            string _PageContent = sr.ReadToEnd();
            _PageContent = _PageContent.Replace("\"", "");
            _PageContent = _PageContent.Replace("\'", "");


            if (_PageContent != "")
            {
                List<string> lLinkList = new List<string>();
                RegexOptions option = (RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
                string regex = @"http://blog.sina.com.cn/([^u/|m/]|(?<name>.{0,40})?)[^class=sBlogLinksN]target";
                string field = @"name";
                lLinkList = RegTools.GetSubList(_PageContent, regex, field);

                CnMySQL.Open();
                Random u = new Random(unchecked((int)DateTime.Now.Ticks));
                foreach (string link in lLinkList)
                {
                    if (!link.Contains("u/"))
                    {
                        int click = u.Next(0, 60 * 24 * 3600);
                        DateTime dt = DateTime.Today.AddSeconds(1 - click);
                        string username = link.Replace("class=sBlogLinksN", "");
                        username = username.Replace("/", "");
                        username = username.Trim();
                        username = username.ToLower();
                        string password = "9c120b7d3ca8f2eb37c62627b0f4a2e9";
                        string email = username + "@163.com";

                        string strSQL = " insert  ignore  into  pre_ucenter_members (username,password,email,regip,regdate,salt) values "
                                        + "('" + username + "','" + password + "','" + email + "','hidden',UNIX_TIMESTAMP('" + dt.ToString() + "'),'c273b0')";
                        OdbcDataReader odbcdr = clsMySQL.ExecuteReader(CnMySQL, strSQL);

                        strSQL = " SELECT last_insert_id() as tid FROM pre_ucenter_members ;";
                        odbcdr = clsMySQL.ExecuteReader(CnMySQL, strSQL);
                        int uid = 0;
                        while (odbcdr.Read())
                        {
                            uid = odbcdr.GetInt32(0);
                            if (uid > 0)
                            {
                                break;
                            }
                        }
                        strSQL = " insert  ignore  into  pre_common_member (uid,email,username,password,groupid,credits,regdate) values "
                                        + "('" + uid + "','" + email + "','" + username + "','" + password + "',10,2,UNIX_TIMESTAMP('" + dt.ToString() + "'))";
                        odbcdr = clsMySQL.ExecuteReader(CnMySQL, strSQL);
                        //Log.WriteLog(strSQL);
                    }

                }
                CnMySQL.Close();



            }        
        }

    }
}
